// pages/todo/todo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: 'pending',
    todos: [
      {
        id: 1,
        title: '完成项目文档',
        description: '编写项目需求文档和API设计文档',
        dueDate: new Date('2024-03-25'),
        priority: 'high',
        completed: false,
        completedAt: null
      },
      {
        id: 2,
        title: '准备周会演示',
        description: '准备项目进度演示PPT',
        dueDate: new Date('2024-03-23'),
        priority: 'medium',
        completed: false,
        completedAt: null
      },
      {
        id: 3,
        title: '回复客户邮件',
        description: '回复客户关于功能变更的邮件',
        dueDate: new Date('2024-03-22'),
        priority: 'high',
        completed: true,
        completedAt: new Date('2024-03-20')
      }
    ],
    dialogVisible: false,
    isEdit: false,
    todoForm: {
      id: '',
      title: '',
      description: '',
      dueDate: new Date(),
      priority: 'medium'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  changeTodoTab(e) {
    this.setData({
      activeTab: e.currentTarget.dataset.tab
    })
  },
  
  get pendingTodos() {
    return this.data.todos.filter(todo => !todo.completed)
  },
  
  get completedTodos() {
    return this.data.todos.filter(todo => todo.completed)
  },
  
  formatDate(date) {
    if (!date) return ''
    
    const d = new Date(date)
    const year = d.getFullYear()
    const month = d.getMonth() + 1
    const day = d.getDate()
    
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`
  },
  
  showAddTodoDialog() {
    this.setData({
      dialogVisible: true,
      isEdit: false,
      todoForm: {
        id: '',
        title: '',
        description: '',
        dueDate: new Date(),
        priority: 'medium'
      }
    })
  }
})